var http = require('http')
var fs = require('fs')
var url = require('url')
//创建服务器
http.createServer(function (request, response) {
    //解析请求
    var pathname = url.parse(request.url).pathname
    fs.readFile(pathname.substr(1), function (err, data) {
        if (err) {
            console.log(err);
        }
        else {
            response.writeHead(200, { 'content-Type': 'text.html' })
            response.write(data.toString())
            response.end()
        }
    })
}).listen(8080) 
//API接口
var request = require('request');
//获取本地时间模块
let time= new Date()
let day=time.toLocaleString()//获取本地时间
console.log('日期'+day);
//剪切处理数据
let arr=day.substr(0,10).split('/')
let n=day.substr(0,10).split('/')[1]
let m=day.substr(0,10).split('/')[2]
if(n<10){//加零
    arr[1]=0+n
}
if(m<10){//加零
    arr[2]=0+m
}

let dayU=arr.join('-')//转化当日时间成功
console.log(dayU);
var url1 = "https://dataapi.joinquant.com/apis";

var requestData = {
    "method": "get_token",
    "mob": "18596012357",
    "pwd": "Lianghua17"  //密码和账号
};
function updata() {
    request(
        {
        url: url1,
        method: "POST",
        body: JSON.stringify(requestData)
    }, 
    function (error, response, token) {
        var requestData = {
            "method": "get_price_period",
            "token": token,
            "code": "IF9999.CCFX",
            "unit": "1m",
            "date": '2021-09-09 '+"09:30:00",
            "end_date":'2021-09-09 '+"15:00:00",
            "fq_ref_date": "2018-12-18"
        };
        request({
            url: url1,
            method: "POST",
            body: JSON.stringify(requestData)
        }, function (error, response, body) {
            console.log('写入文件');
            fs.writeFile('ipit.txt',body, function (error) {//把body写如到ipit的记事本
                if (error) {
                    return console.error(error);
                }
                console.log('数据写入成功');
                console.log('------');
                console.log('读取文件数据');
            })
        });
    });
}
updata()
setInterval(updata,20000)